<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="记录人" align="center" prop="createBy" width="120" />
      <el-table-column label="记录日期" align="center" prop="recordTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否完成" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <span :style="{'color': colors(scope.row.status)}">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实施内容" align="center" prop="content" width="300" />
      <el-table-column label="图片" align="center" prop="imageUrl" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.imageArr"
                    style="width: 100px; height: 100px; margin-right: 15px"
                    :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" width="120" />
      <el-table-column label="更新日期" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width" fixed="right" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="实施内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入实施内容" />
        </el-form-item>
        <el-form-item label="是否完成" prop="status">
          <el-select v-model="form.status">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-date-picker v-model="form.recordTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择记录时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="上传图片" prop="imageUrl">
          <ImageUpload v-model="form.imageUrl" :limit="100"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update } from "@/api/social/program/programRecord";
import {blurRule, changeRule} from "@/utils/formRules";

export default {
  name: "ProgramRecord",
  dicts: [],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      rowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content:blurRule('请输入实施内容'),
        status:changeRule('请选择是否完成'),
        recordTime:changeRule('请选择记录时间')
      },
      imageColWidth:160
    };
  },
  created() {
    this.id = this.$route.params && this.$route.params.id;
    this.getList();
  },
  methods: {
    colors(status) {
      if (status === "是") {
        return '#2a9c4a'
      } else {
        return '#fe9f29'
      }
    },
    handleExport() {
      this.download('social/program/programRecord/export', {
        ...this.queryParams
      }, `项目进度列表_${new Date().getTime()}.xlsx`)
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.programId = this.id;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        let maxWith = 1;
        response.rows.map(item=>{
          item.imageArr=this.handleImg(item)
          if (item.imageArr.length > maxWith) {
            maxWith = item.imageArr.length;
          }
        })
        this.imageColWidth = maxWith * 160;
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    //处理图片显示
    handleImg(item){
      let imageArr=[]
      if (item.imageUrl ) {
        imageArr = item.imageUrl.split(",");
      }
      return imageArr
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {};
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.programId = this.id;
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
